package com.binance.mbxgateway.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.binance.master.models.APIRequest;
import com.binance.master.models.APIResponse;
import com.binance.mbxgateway.api.IProductApi;
import com.binance.mbxgateway.vo.PriceConvertVo;
import com.binance.mbxgateway.vo.request.AddProductRequest;
import com.binance.mbxgateway.vo.request.CreateAssetRequest;
import com.binance.mbxgateway.vo.request.IceicebergLimitRequest;
import com.binance.mbxgateway.vo.request.NumAlgoOrdersRequest;
import com.binance.mbxgateway.vo.request.OrderTypeRequest;
import com.binance.mbxgateway.vo.request.PriceConvertRequest;
import com.binance.mbxgateway.web.service.IProductService;

@RestController
public class ProductController implements IProductApi {

    @Autowired
    private IProductService productService;

    @Override
    public APIResponse<PriceConvertVo> priceConvert(@Validated @RequestBody APIRequest<PriceConvertRequest> request)
            throws Exception {
        PriceConvertVo result = this.productService.internalConvert(request.getBody().getFrom(),
                request.getBody().getTo(), request.getBody().getAmount(), request.getBody().getDate());
        return APIResponse.getOKJsonResult(result);
    }

    @Override
    public APIResponse<Void> setNumAlgoOrdersFilter(@Validated @RequestBody APIRequest<NumAlgoOrdersRequest> request)
            throws Exception {
        this.productService.setNumAlgoOrdersFilter(request.getBody().getSymbol(), request.getBody().getNumOrders());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> setIceicebergLimit(@Validated @RequestBody APIRequest<IceicebergLimitRequest> request)
            throws Exception {
        this.productService.setIceicebergLimit(request.getBody().getSymbol(), request.getBody().getLimit());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> setProductType(@Validated @RequestBody APIRequest<OrderTypeRequest> request)
            throws Exception {
        this.productService.setProductOrderTypes(request.getBody());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> addProduct(@Validated @RequestBody APIRequest<AddProductRequest> request)
            throws Exception {
        this.productService.addProduct(request.getBody());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<Void> creatAsset(@Validated @RequestBody APIRequest<CreateAssetRequest> request)
            throws Exception {
        this.productService.creatAsset(request.getBody().getSymbol(), request.getBody().getDecimalPlaces());
        return APIResponse.getOKJsonResult();
    }
}
